package com.petmarket.client;

import com.petmarket.common.CommonClient;
import com.petmarket.common.PageResult;
import com.petmarket.contract.TransferContract;
import com.petmarket.entity.Transfer;
import com.petmarket.uitls.SpringUtils;
import org.fisco.bcos.sdk.v3.BcosSDK;
import org.fisco.bcos.sdk.v3.codec.datatypes.generated.tuples.generated.Tuple4;
import org.fisco.bcos.sdk.v3.model.TransactionReceipt;
import org.fisco.bcos.sdk.v3.transaction.model.exception.ContractException;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Service;

import java.math.BigInteger;
import java.util.ArrayList;
import java.util.List;

@Service
public class TransferClient extends CommonClient implements ApplicationRunner {
    @Override
    public void run(ApplicationArguments args) throws Exception {
        BcosSDK sdk = SpringUtils.getBean("bcosSDK");
        deploy("Record", TransferContract.class, sdk);
    }


    public PageResult<Transfer> getPetPage(Integer pageNo, Integer pageSize,String buyerId) throws ContractException {
        PageResult<Transfer> pageResult = new PageResult<>();
        BigInteger start = BigInteger.valueOf((pageNo - 1) * pageSize);
        BigInteger end = BigInteger.valueOf(pageNo * pageSize);
        System.out.println("start:" + start + "end:" + end);
        List<Transfer> petList = new ArrayList<>();
        int count = 0,begin = start.intValue() + 1,last = end.intValue();
        while(count < pageSize){
            Transfer transfer = this.queryInner(BigInteger.valueOf(begin));

            if(transfer.getIsDeleted().equals("")){
                break;
            }
            else if (transfer.getIsDeleted().equals("1")||!transfer.getBuyerId().equals(buyerId)){
                begin++;
                continue;
            }



            petList.add(transfer);
            count++;
            begin++;
        }
        pageResult.setRecords(petList);
        return pageResult;
    }


    private Transfer queryInner(BigInteger id) throws ContractException {
        TransferContract recordContract = (TransferContract) getContractMap().get("Record");
        Tuple4< String, String, String,String> getValue = recordContract.select(id);
//        Pet pet = new Pet();
//        pet.setId(id);
//        pet.setName(getValue.getValue1());
//        pet.setType(getValue.getValue2());
//        pet.setSellerId(getValue.getValue3());
//        pet.setPetPrice(getValue.getValue4());
//        pet.setStatus(getValue.getValue5());
//        pet.setImgUrl(getValue.getValue6());
//        pet.setIsDeleted(getValue.getValue7());

        Transfer transfer =new Transfer();
        transfer.setId(id);
        transfer.setBuyerId(getValue.getValue1());
        transfer.setSellerId(getValue.getValue2());
        transfer.setAmount(getValue.getValue3());
        transfer.setIsDeleted(getValue.getValue4());

        return transfer;
    }


    public void insert(Transfer transfer) throws ContractException {
        TransferContract transferContract = (TransferContract) getContractMap().get("Record");
        BigInteger count = transferContract.count(BigInteger.valueOf(0), BigInteger.valueOf(200)).add(BigInteger.valueOf(1));
        TransactionReceipt receipt = transferContract.insert(count,transfer.getBuyerId(),transfer.getSellerId(),transfer.getAmount(),"0");
        logger.info("调用TransferClient的insert方法");
        logger.info("结果：{}", receipt);
    }

    public void remove(Transfer transfer){
        TransferContract transferContract = (TransferContract) getContractMap().get("Record");
        TransactionReceipt receipt = transferContract.remove(transfer.getId());
        logger.info("调用UserClient的remove方法");
        logger.info("结果：{}", receipt);
    }


}
